<?php
class sprder  {
	//id
	protected $_id            = "";
	//URL
	protected $_url           = "";
	//Max time
	protected $timeout        = 60;
	//主URL
	protected $host           = "";
	//cookies信息
	protected $cookies        = "";
	//分块
	protected $blockDelimiter = "";
	//获取价格正则
	protected $pricePattern   = "";
	//Title 正则
	protected $titlePattern   = "";
	//详细链接 正则
	protected $hrefPattern    = "";
	//图片信息
	protected $imgPattern     = "";
	//
	protected $pagePattern    = "";
	//当前页
	protected $currentPage    = 1;
	//分页信息
	protected $pageInfo       = array();
	//是否有设置分页
	protected $setPage        = null;
	
	function __construct($arr) {
		
		$this->_id            = $arr['id'];
		
		$this->_url           = $arr['webSite'];
		
		//$this->_url           = 'http://www.embryform.com/popup.asp?lang=3&ProductId=EY5082';
		
		$this->blockDelimiter = $arr["blockDelimiter"];
		
		/*$this->pricePattern   = "/<\/div><span\s+class=\"prodSale\">(.+)<\/span><div\s+class='off'>/";
		$this->titlePattern   = "/<div\s+class=\"prodName\"><font\s+color=\"#555\">(.+)<\/font><\/div><div/";
		$this->hrefPattern    = "/<a\s+target='_blank'\s+href=\"(.+)\"><img\s+alt=/";
		$this->imgPattern     = "/src=\"(.+)\"\s+\/><span\s+class=\"PriceBox\">/";
		$this->pagePattern    = "";*/
		$this->pricePattern = $arr["pricePattern"];
		$this->titlePattern = $arr["titlePattern"];
		$this->hrefPattern  = $arr["hrefPattern"];
		$this->imgPattern   = $arr["imgPattern"];
		$this->pagePattern  = $arr["pagePattern"];
		$this->setPage      = $arr["setPage"];
		
	}
	/**
	 * 
	 * 抓取程序入口
	 */
	function run(){
		
		while(true){
			//确认要搜索的
			$url = $this->dividePageInfo();
			
			if($url == false){
				
				break;
			}
			
			$this->wlog($url, "url.txt",false);
			
			$cts = $this->getWebContent($url);
			
			//preg_match("/<TD\s+VALIGN=TOP\s+width='290'><IMG\s+SRC=\"(.*)\"\s+WIDTH=\d+\s+HEIGHT=\d+\s+ALT=\"\"><\/TD>/",$cts,$rs);
			
			//print_r($rs);
			
			//exit;
			
			$cts = $this->trimBody($cts);
			
			//获取分页信息
			$this->getPateInfo($cts);
			
			//切分成数组
			//print_r($cts);exit;
			//$this->blockDelimiter = "/<dl\s+class=\"list-box\">/";
			
			//echo $this->blockDelimiter;exit;
			$cts = $this->splitSection($cts, $this->blockDelimiter);
			
			if(empty($cts)){
				
				break;
				
			}
			//提取要用的信息
			$this->getDetail($cts);
			
			echo $this->currentPage."<br/>";
				
		}
	}
	/**
	 * 分解分页信息
	 */
	private function getPateInfo($str){
		if($this->setPage!=null){
			return false;
		}
		
		preg_match_all($this->pagePattern,$str,$pages);
		
		array_shift($pages);
		
		foreach($pages[0] as $k=>$v){
			
			$this->pageInfo[$pages[1][$k]] = $v;
			
		}
		
	}
	/**
	 * 解析分页URL
	 */
	private function dividePageInfo(){
		if($this->setPage == null){
			
			foreach($this->pageInfo as $k=>$v){
				
				$endPage = $k;
				
			}
			
			if($endPage == $this->currentPage){
				return false;
			}
			
			foreach($this->pageInfo as $k=>$v){
				if($k==$this->currentPage){
					$k++;
					$this->_url        = $this->pageInfo[$k];
					$this->currentPage = $k;
					break;
				}
			}
			
			return $this->_url;
			
		}
		if($this->currentPage == 1){
			$this->currentPage++;
			return $this->delPageFlag();
		}
		preg_match("/{(.+([0-9]+))}/",$this->_url,$pageNo);
		if(is_array($pageNo)&&!empty($pageNo)){
			//$pageNo[2]++;
			$param = preg_replace("/[0-9]+/",$this->currentPage,$pageNo[1]);
			$url = preg_replace("/{.+}/",$param,$this->_url);
			$this->currentPage++;
			return $url;
		}
		return false;
	}
	/**
	 * 去掉分页标记
	 */
	private function delPageFlag(){
		$url = preg_replace("/[{}]/","",$this->_url);
		return $url;
	}
	/**
	 * 分解获取详细信息
	 * @param $arr
	 * @param void
	 */
	private function getDetail($arr){
		if(!is_array($arr)||empty($arr)){
			return false;
		}
		foreach($arr as $k=>$v){
			//下载保存图片
			preg_match($this->imgPattern, $v,$imgs);
			
			//下载
			$this->downFile($imgs[1]);
			
			//取价格信息
			preg_match($this->pricePattern, $v,$price);
			
			//取title信息
			preg_match($this->titlePattern,$v,$title);
			
			//取产品详细连接信息
			preg_match($this->hrefPattern,$v,$href);
			
			$arrRs['imgPath'] = isset($imgs['1'])?$imgs['1']:'';
			
			$arrRs['price']   = isset($price['1'])?$price['1']:'';
			
			$arrRs['title']   = isset($title['1'])?$title['1']:'';
			
			$arrRs['detailHref']    = isset($href['1'])?$href['1']:'';
			
			//print_r($arrRs);exit;
			//保存
			$this->saveData($arrRs);
			unset($imgs,$price,$title,$href);
		}
	}
	/**
	 * 保存抓取内容保存到数据库
	 * @param Array $arr
	 * $arr['imgPath']
	 * $arr['price']
	 * $arr['title']
	 * $arr['href']
	 * @param boolean
	 */
	private function saveData($arr){
		if(empty($arr)){
			return false;
		}
		$db = new brand_products();
		
		$imgPath = $arr['imgPath'];
		$price = $arr['price'];
		$detailHref = $arr['detailHref'];
		$name = $arr['title'];
		$sql = "insert into `ydmx_brand_products` (`brandId`,`price`,`detailHref`,`name`,`imgPath`,`createDate`,`deleted`)";
		$sql .= " values ('".$this->_id."','".$price."','".$detailHref."','".$name."','".$imgPath."','".date('Y-m-d H:i:s')."','0')";
		$arr = array(
				'brandId'=>$this->_id,
				'price'=>$price,
				'detailHref'=>$detailHref,
				'name'=>$name,
				'imgPath'=>$imgPath,
				'createDate'=>date('Y-m-d H:i:s'),
				'deleted'=>'0'
		);
		
		return $db->insertData($arr);
		
		//return $db->query($sql);
	}
	/**
	 * 
	 * 下载并保存图片文件到相应的目录
	 * @param String $url
	 * @param void
	 */
	protected  function downFile($url){
		//检查文件是否存 在
		
		$dir = "upfiles/".$this->_id;
		if(!is_dir($dir)){
			mkdir($dir,777);
		}
		$name = explode('/', $url);
		$name = end($name);
		$name = $dir."/".$name;
		$fp   = @fopen($name, 'a');
		if($fp==false){
			echo $url."<br/>";
			return false;
		}
		
		$img = $this->getWebContent($url);
		fputs($fp, $img, strlen($img));
		fclose($fp);
		
		return $name;
	}
	/**
	 * 
	 * 获取抓取的页面内容
	 * @param String $url
	 * @param String
	 */
	protected function getWebContent($url){
		
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookies);
       // curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);                
        curl_setopt($ch,CURLOPT_HEADER,0);        
        $str = curl_exec($ch);
        if(empty($str)){
        	
        	$str = file_get_contents($url);
        }
        $this->wlog($str,'1.txt'); 
        curl_close($ch);
        return $str;
	}
	/**
	 * 
	 * 获取抓取的页面内容
	 * @param String $url
	 * @param String
	 */
	protected function postWebContent($url,$data){
		
	    $ch = curl_init();    
	    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);  
	    curl_setopt($ch, CURLOPT_URL,$url);     
	    curl_setopt($ch, CURLOPT_POST, 1 );     
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);     
	    $data = curl_exec($ch);        
	    curl_close($ch);      
	    if ($data){   
	        return $data;
	    }else{
	    	$url .= $data;
	    	$data = file_get_contents($url);
	        if($data){
	        	return $data;
	        }else{
	        	return false;
	        }
	    } 
	}
	/**
	 * 
	 * 去除Body元素
	 * @param $str
	 * @return String
	 */
	function trimBody($str){
		$rs = preg_split("/<body.+>/i",$str);
		
		$str = $rs[1];
		
		$rs = preg_split("/<\/\s*body\s*>/",$str);
		
		return $rs[0];
	}
	/**
	 * 
	 * 将要取的内容分段提取
	 * @param $str
	 * @param $delimiter
	 */
	function splitSection($str,$delimiter,$type=0){
		//echo $delimiter;exit;
		if($type == 0){
			$rs = preg_split($delimiter, $str);
		}else if($type = 1){
			$rs = explode($delimiter,$str);	
		}
		
		array_shift($rs);
		array_pop($rs);
		return $rs;
	}
	//写日志
	private function wlog($str,$file,$mode=true){
		$baseDir = dirname(__FILE__).DIRECTORY_SEPARATOR;
		
		$m = ($mode == true)?'w':'a+';
		$file = empty($file)?"logs.txt":$file;
		$file = $baseDir.'../../runtime/'.$file;
		//echo $file;exit;
		$fp = fopen($file, $m);
		fwrite($fp, $str);
		fclose($fp);
	}
	function __destruct() {

	
	}
}


?>